<?php
 Class ShopAction extends Action{
 	public function index(){
 		$db=M();
 		Import('ORG.Util.Page');
 		$sql1="select count(*) tot from shop";
 		$re=$db->query($sql1);
 		$count=$re[0]['tot'];
 		$page=new Page($count,10);
 		$show=$page->show();
 		$sql2="select s.* ,b.name bname,c.name cname from shop  s
 						left join brand b on b.id=s.brand_id
 						left join class c on c.id=s.class_id
 						limit {$page->firstRow},{$page->listRows}
 						";
 		$rows=$db->query($sql2);
 		$this->assign('show',$show);
 		$this->assign('rows',$rows);
 		$this->display();
 	}
 	// 添加模版
 	public function add(){
 		$class=M('Class');
 		$classrows=$class->select();

 		$brand=M('Brand');
 		$brandrows=$brand->select();

 		$this->assign('classrows',$classrows);
 		$this->assign('brandrows',$brandrows);
 		$this->display();
 	}
 	// 添加操作
 	public function add_handle(){
 		// echo '<pre>';
 		// print_r($_POST);
 		// print_r($_FILES);exit;
 		Import('ORG.Net.UploadFile');
 		$upload = new UploadFile();// 实例化上传类
 		$upload->maxSize  = 3145728 ;// 设置附件上传大小
 		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
 		$upload->savePath ='./Public/upload/shop/';// 设置附件上传目录

 		if(!$upload->upload()) {// 上传错误提示错误信息
 		$this->error($upload->getErrorMsg());
 		}else{// 上传成功 获取上传文件信息
 		$info =  $upload->getUploadFileInfo();
 		
 	
 		// 图片缩略
 		Import('ORG.Util.Image');
 		$file=$info[0]['savepath'].$info[0]['savename'];
 		$thumb=$info[0]['savepath'].'t_'.$info[0]['savename'];
 		Image::thumb($file,$thumb,'','220','200');

 		// 删除原图
 		$_POST['img']=$info[0]['savename'];
 		
 		} 

 		$shop=M('Shop');
 		$class_brand=explode('-',$_POST['class_brand']);//拆分获得分类id和品牌id
 		$_POST['class_id']=$class_brand[0];
 		$_POST['brand_id']=$class_brand[1];
 		// echo '<pre>';print_r($_POST);exit;
 		if($shop->create()){
 			$shop->add();
 			$this->redirect('index');
 		}else{
 			$this->error('添加失败');
 		};
 	}

 	// 删除
 	public function delete(){
 		$shop=M('Shop');
 		// 删除图片
 		$img='./Public/upload/shop/'.$_GET['img'];
 		$t_img='./Public/upload/shop/'.'t_'.$_GET['img'];
 		unlink($img);
 		unlink($t_img);
 		// 获取$.get传过来的id
 		$id=$_GET['id'];
		$re=$shop->delete($id);
		if($re){
			$this->redirect('index');
		}
 		
 		
 	}


 	// 修改模版
 	public function edit(){
 		$shop=M('Shop');
 		$id=$_GET['id'];
 		$shoprow=$shop->find($id);

 		$brand=M('Brand');
 		$brandrows=$brand->select();

 		$class=M('Class');
 		$classrows=$class->select();
 		$this->assign('shoprow',$shoprow);
 		$this->assign('brandrows',$brandrows);
 		$this->assign('classrows',$classrows);
 		$this->display();
 	}
 	// 修改操作
 	public function edit_handle(){
 		
 		Import('ORG.Net.UploadFile');
 		$upload=New UploadFile();
 		$upload->maxSize=3500000;
 		$upload->allowExts=array('jpg','jpeg','png','gif');
 		$upload->savePath='./Public/upload/shop/';
 		if($upload->upload()){//上传成功
 			$info=$upload->getUploadFileInfo();
 			// 图片缩略
 			Import('ORG.Util.Image');
 			$image=$info[0]['savepath'].$info[0]['savename'];
 			$thumb=$info[0]['savepath'].'t_'.$info[0]['savename'];
 			$thumb= Image::thumb($image,$thumb,'','220','200');
 			$_POST['img']=$info[0]['savename'];
 		}
 		$shop=M('Shop');
 		// 删除原图及原缩略图
 		$oldImg=$info[0]['savepath'].$_POST['imgsrc'];
 		$t_oldImg=$info[0]['savepath'].'t_'.$_POST['imgsrc'];
 		unlink($oldImg);
 		unlink($t_oldImg);
 		$class_brand=explode('-',$_POST['class_brand']);//拆分获得分类id和品牌id
 		$_POST['class_id']=$class_brand[0];
 		$_POST['brand_id']=$class_brand[1];
 		if($shop->create()){
 			$shop->save();
 			$this->redirect('index');
 		};
 	}
 }
?>